#!/bin/bash

echo "Testing Fixed EbbFlow with Real ADKR"
echo "===================================="

# Clean logs
rm -rf logs/*
mkdir -p logs

# Configuration
NODES=4
EPOCHS=10
R=3
BATCH_SIZE=1000
TEST_DURATION=30

echo "Test Configuration:"
echo "  - Nodes: $NODES"
echo "  - Epochs: $EPOCHS"
echo "  - ADKR Interval (R): $R"
echo "  - Batch Size: $BATCH_SIZE"
echo "  - Duration: ${TEST_DURATION} seconds"
echo ""

# === Test Fixed EbbFlow ===
echo "=== Testing Fixed EbbFlow Protocol ==="
cd /home/ebbflow

# Start EbbFlow servers
for i in $(seq 0 $((NODES-1))); do
    ./bin/ebbflow_server -id $i -n $NODES -f 1 > logs/ebbflow_server_${i}.log 2>&1 &
    echo "Started EbbFlow server $i"
    sleep 0.1
done

sleep 3

echo "Starting EbbFlow client..."
./bin/ebbflow_client -id 100 -batch $BATCH_SIZE -count 10000 -txsize 250 > logs/ebbflow_client.log 2>&1 &
EBBFLOW_CLIENT_PID=$!

echo "Running Fixed EbbFlow test for ${TEST_DURATION} seconds..."
sleep $TEST_DURATION

echo "Stopping EbbFlow processes..."
killall ebbflow_server > /dev/null 2>&1
kill $EBBFLOW_CLIENT_PID > /dev/null 2>&1
sleep 2

echo ""

# === Performance Analysis ===
echo "=== Fixed EbbFlow Performance Analysis ==="
echo ""

echo "EbbFlow Performance:"
echo "-------------------"
if [ -f "logs/ebbflow_client.log" ]; then
    SUBMITTED=$(grep "Submitted:" logs/ebbflow_client.log | awk '{print $2}')
    ELAPSED=$(grep "Submitted:" logs/ebbflow_client.log | awk '{print $5}')
    SUBMISSION_TPS=$(grep "Submission TPS:" logs/ebbflow_client.log | awk '{print $4}')
    
    echo "  - Transactions Submitted: ${SUBMITTED:-0}"
    echo "  - Submission Time: ${ELAPSED:-0} seconds"
    echo "  - Submission TPS: ${SUBMISSION_TPS:-0}"
    
    # Check for crashes
    CRASH_COUNT=$(grep -c "panic" logs/ebbflow_server_0.log)
    echo "  - Server Crashes: ${CRASH_COUNT:-0}"
    
    # Count epoch activities
    EPOCH_COUNT=$(grep -c "epoch" logs/ebbflow_server_0.log)
    ADKR_COUNT=$(grep -c "ADKR" logs/ebbflow_server_0.log)
    ACS_COUNT=$(grep -c "ACS" logs/ebbflow_server_0.log)
    
    echo "  - Epoch Activities: ${EPOCH_COUNT:-0}"
    echo "  - ADKR Executions: ${ADKR_COUNT:-0}"
    echo "  - ACS Executions: ${ACS_COUNT:-0}"
    
    # Check for real ADKR execution
    REAL_ADKR_COUNT=$(grep -c "Real ADKR" logs/ebbflow_server_0.log)
    echo "  - Real ADKR Executions: ${REAL_ADKR_COUNT:-0}"
    
    # Check for epoch management
    EPOCH_MGMT_COUNT=$(grep -c "epoch management" logs/ebbflow_server_0.log)
    echo "  - Epoch Management Activities: ${EPOCH_MGMT_COUNT:-0}"
else
    echo "  - EbbFlow client log not found"
fi

echo ""
echo "Server Logs Analysis:"
echo "--------------------"
for i in $(seq 0 $((NODES-1))); do
    if [ -f "logs/ebbflow_server_${i}.log" ]; then
        echo "Server $i:"
        echo "  - Epochs: $(grep -c "epoch" logs/ebbflow_server_${i}.log)"
        echo "  - ADKR: $(grep -c "ADKR" logs/ebbflow_server_${i}.log)"
        echo "  - ACS: $(grep -c "ACS" logs/ebbflow_server_${i}.log)"
        echo "  - Crashes: $(grep -c "panic" logs/ebbflow_server_${i}.log)"
        echo "  - Real ADKR: $(grep -c "Real ADKR" logs/ebbflow_server_${i}.log)"
    fi
done

echo ""
echo "=== Fixes Applied ==="
echo "✅ Copied Turritopsis ADKR to EbbFlow"
echo "✅ Fixed nil map panic in connection.go"
echo "✅ Added EnsureConnectionMap() function"
echo "✅ Updated EbbFlow to use real ADKR"
echo "✅ Enhanced epoch management integration"

echo ""
echo "Fixed EbbFlow test completed."


